import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from wxxcx.items import WxxcxItem


class WwxcxSpiderSpider(CrawlSpider):
    name = 'wwxcx_spider'
    allowed_domains = ['wxapp-union.com']
    start_urls = ['http://www.wxapp-union.com/portal.php?mod=list&catid=1&page=1']

    rules = (
        Rule(LinkExtractor(allow=r'.+mod=list&catid=1&page=\d+'),  follow=True),
        Rule(LinkExtractor(allow=r'.+article-.+\.html'),callback='parse_item',follow=False)
    )

    def parse_item(self, response):
        #item = {}
        #item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
        #item['name'] = response.xpath('//div[@id="name"]').get()
        #item['description'] = response.xpath('//div[@id="description"]').get()
        #return item
        title = response.xpath('//h1[@class= "ph"]/text()').get()
        author = response.xpath('//p[@class= "authors"]/a/text()').get()
        time = response.xpath('//p[@class= "authors"]/span/text()').get()
        content = ''.join(response.xpath('//td[@id="article_content"]//text()').getall()).strip()
        item = WxxcxItem(title=title, author=author, time=time, content=content)
        yield  item
